package com.example.bysj.Dao;
//201902104061徐中堂
import com.example.bysj.domain.authority.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
//声明数据库访问类
@Repository("userAssRole")
public class UserAssRoleDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private RoleDao roleDao;
    //根据id查找角色
    public List<Role> getRoleByUserId(int user_id){
        String sql = "SELECT * FROM userAssRole where user_id=?";
        List<Role> roles = jdbcTemplate.query(sql, new Object[]{user_id}, new RowMapper<Role>() {
            @Override
            public Role mapRow(ResultSet rs, int rowNum) throws SQLException {
                return roleDao.getRoleById(rs.getInt("role_id"));
            }
        });
        return roles;
    }
}
